require(['./config'], () =>{
    require(['template','header','footer'],(template,header)=>{

        class ShopCar{
            constructor (){
                
                this.init()
            }
            init(){
                let cartList = localStorage.getItem('cart')
                if (cartList){
                    this.cartList = JSON.parse(cartList)
                    $('#will_buy_shop').html(template('shopTemplate',{cartList:this.cartList}))
                    this.calcTotalMoney()
                    this.checksChagne()
                    this. allcheeck()
                    this.addNum()
                    this.minusNum ()
                    this.changeNum()
                    this.clearCurrentShop ()
                    this.alreadyBuyShop()
                }else{
                    $('#nothing_shop').show()
                    $('#already_shop').hide()
                }
            }
            calcTotalMoney () {
                $('#Paymoney').html(this.cartList.reduce((money, cart) => {
                  if (cart.check) money += cart.price * cart.num
                  return money
                }, 0).toFixed(2))
              }

              //全选
            allcheeck(){
                const that = this
                $('#all_check').prop('checked',that.cartList.every(cart =>{
                    return cart.check
                })).on('change',function(){

                    $('#will_buy_shop .shop_cont input:checkbox').prop('checked',this.checked)
                    that.cartList = that.cartList.map(cart =>{
                        cart.check = this.checked  
                        return cart
                    })
                   
                    localStorage.setItem('cart',JSON.stringify(that.cartList))
                    // console.log(that.cartList)
                    that.calcTotalMoney ()
                }) 
                //   localStorage.setItem('cart',JSON.stringify(that.cartList))
                // that.calcTotalMoney ()
            }

            // 单选
            checksChagne(){
                const that = this
                $('#will_buy_shop .shop_cont input:checkbox').on('change',function(){
                    
                    let id = $(this).parents('tr').attr('data_id')
                    
                        that.cartList = that.cartList.map(cart =>{
                            if(cart.id == id)cart.check = $(this).prop('checked')
                            return cart
                        })
                    localStorage.setItem('cart',JSON.stringify(that.cartList))
                  
                    $('#all_check').prop('checked',that.cartList.every(cart =>{
                        return cart.check
                    }))
                      localStorage.setItem('cart',JSON.stringify(that.cartList))
                    that.calcTotalMoney ()
                })
              }
              
              //数量加
              addNum (){
                  
                  const that = this
                $('.plus').on('click',function(){
                    // console.log(123)
                    let id = $(this).parents('tr').attr('data_id')
                    that.cartList = that.cartList.map(cart =>{
                        if(cart.id == id){
                            cart.num ++
                            $(this).prev().val(cart.num)
                            
                            $(this).parents('tr').find('.single_price').html((cart.price*cart.num).toFixed(2))
                        }
                        return cart
                    })
                    localStorage.setItem('cart',JSON.stringify(that.cartList))
                    that.calcTotalMoney ()
                    header.shopNum()
                })

              }
            //数量相减

            minusNum (){
                const that = this
              $('.minus').on('click',function(){
                  let id = $(this).parents('tr').attr('data_id')
                  that.cartList = that.cartList.map(cart =>{
                      if(cart.id == id){
                          cart.num --
                          if(cart.num <0 )cart.num=0
                          $(this).next().val(cart.num)
                          $(this).parents('tr').find('.single_price').html((cart.price*cart.num).toFixed(2))
                          
                        }
                      return cart
                  })
                  
                  localStorage.setItem('cart',JSON.stringify(that.cartList))
                  header.shopNum()
                  that.calcTotalMoney ()
              })

            }
            //input keyup 事件
            changeNum (){
                const that = this
                $('.change_num').on('keyup',function(){
                    let id = $(this).parents('tr').attr('data_id')
                    that.cartList = that.cartList.map(cart =>{
                        if(cart.id == id){
                            cart.num = Number($(this).val())
                            if(cart.num <0)cart.num = 0
                            $(this).parents('tr').find('.single_price').html((cart.price*cart.num).toFixed(2))
                        }
                        return cart
                    })
                    localStorage.setItem('cart',JSON.stringify(that.cartList))
                    header.shopNum()
                    that.calcTotalMoney ()
                })

            }

            //删除商品
            clearCurrentShop (){
                    const that = this
                $('.clear_current_shop').on('click',function(){
                   if(confirm('您确定删除此商品吗?Are you sure clear this shop')){
                    let id = $(this).parents('tr').attr('data_id')
                    that.cartList = that.cartList.map((cart,index) =>{
                        if(cart.id == id){ 
                            that.cartList.splice(index, 1)
                            // console.log(that.cartList.length)
                            
                            localStorage.setItem('cart',JSON.stringify(that.cartList))  
                        }
                        if(that.cartList.length === 0){
                            $('#nothing_shop').show()
                            $('#will_buy_shop').hide()
                            $('#already_shop').hide()
                            localStorage.removeItem('cart')  
                        }
                        header.shopNum()
                    })
                  
                   }
                   that.init()
                   that.calcTotalMoney ()
                  
                })
            }
            // 买商品
            alreadyBuyShop(){
                
                const that = this
                $('#indent').on('click',function(){
                    let usersInfo = localStorage.getItem('usersInfo')
                    console.log(usersInfo)
                    if(usersInfo) {
                    let id = $(this).parents('tr').attr('data_id')
                   
                    that.cartList = that.cartList.map((cart,index)=>{
                        if(cart.check){
                            if(confirm('确认购买？')) {
                            that.cartList.splice(index)
                            localStorage.setItem('cart',JSON.stringify(that.cartList))
                            $('#nothing_shop').hide()
                            $('#will_buy_shop').hide()
                            $('#already_shop').show()  
                            header.shopNum()
                            that.calcTotalMoney ()
                        }
                    }
                    
                        if(that.cartList.length === 0){    
                            localStorage.removeItem('cart') 
                            $('#nothing_shop').show()
                            $('#will_buy_shop').hide()
                            $('#already_shop').hide()
                        }
                       
                    })
                    that.init()
                
             
            }else{
                alert('您还没有登录')
            }
            })
        }


    }

        new ShopCar()


    })
})